package jinyilw.swing.visuals.panel;

import java.awt.Composite;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;

import javax.swing.JComponent;

import jinyilw.swing.ColorTools;
import jinyilw.swing.ComponentTools;
import jinyilw.swing.rlaf.theme.RRootPaneSet;
import jinyilw.swing.rlaf.utils.RLafUtils;

public class FlatImagePanel extends JComponent // 图像面板
{
	private static final long serialVersionUID = 1005671082158481413L;
	private static RRootPaneSet rootPaneSet;
	protected Image customImage;
	protected float customAlpha = 0;

	public FlatImagePanel()
	{
		rootPaneSet = RRootPaneSet.getRootPaneSet();
	}

	public void setCustomImage(Image img)
	{
		customImage = img;
		if (isShowing())
			repaint();
	}

	public void setCustomAlpha(float alpha)
	{
		customAlpha = alpha;
		if (isShowing())
			repaint();
	}

	public Image getCustomImage()
	{
		return customImage;
	}

	@Override
	protected void paintComponent(Graphics g)
	{
		int width = getWidth(), height = getHeight();
		Graphics2D g2d = (Graphics2D) g;
		Composite composite = ComponentTools.applyAlpha(g2d,
				customAlpha > 0 ? customAlpha : RLafUtils.universeAlpha);
		g2d.drawImage(customImage != null ? customImage : rootPaneSet.bgImage,
				0, 0, width, height, null);
		if (composite != null)
			g2d.setComposite(composite);
		int brightness = RLafUtils.universeBright;
		if (brightness > 10)
		{
			g2d.setColor(
					ColorTools.getAlphaColor(rootPaneSet.bgColor, brightness));
			g2d.fillRect(0, 0, width, height);
		}
	}
}
